package com.heaboy.feedback.websocket.handler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.heaboy.feedback.constant.EnumAttendanceStatus;
import com.heaboy.feedback.domain.StudentAttendance;
import com.heaboy.feedback.service.IStudentAttendanceService;
import com.heaboy.feedback.websocket.WsWebSocketHandler;
import com.heaboy.feedback.websocket.domain.Message;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class StudentMessageHandler implements MessageHandler{

    @Autowired
    private WsWebSocketHandler wsWebSocketHandler;
    @Autowired
    private IStudentAttendanceService studentAttendanceService;

    @Override
    public void handle(Message msg) {
        switch (msg.getType()) {
            case 0:
                //签到
                StudentAttendance studentAttendance = new StudentAttendance();
                studentAttendance.setId(msg.getBusinessId());
                studentAttendance.setStatusCode(EnumAttendanceStatus.END.getCode());
                studentAttendance.setSignInTime(DateUtils.getNowDate());
                studentAttendanceService.updateStudentAttendance(studentAttendance);
                break;
            case 1:
                //常见问题
                break;
            case 2:
                //自定义问题
                break;
            default:
        }
        ObjectMapper mapper = new ObjectMapper();
        try {
            String s = mapper.writeValueAsString(msg);
            wsWebSocketHandler.sendToTeacher(msg.getLessonId().toString(),s);
        } catch (JsonProcessingException e) {
//            throw new RuntimeException(e);
        }

    }
}
